package com.hui.account.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hui.account.R;
import com.hui.account.database.SQLManage;
import com.hui.account.entity.RecordEntity;
import com.hui.account.entity.UserEntity;
import com.hui.account.utils.CommonUtils;
import java.util.Calendar;
import java.util.List;

/**
 * 欠款记录适配器
 */
public class RecordAdapter extends BaseAdapter {

    Context context;

    List<RecordEntity> data; // 欠款记录

    LayoutInflater inflater; // 获取视图

    int year, month, day;

    public RecordAdapter(Context context, List<RecordEntity> data) {
        this.context = context;
        this.data = data;
        inflater = LayoutInflater.from(context);

        // 获取当前年月日
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        day = calendar.get(Calendar.DAY_OF_MONTH);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    /**
     * 获取视图
     */
    @SuppressLint({"ViewHolder", "SetTextI18n", "ResourceAsColor"})
    @Override
    public View getView(int i, View view, ViewGroup parent) {
        // 1、获取视图
        view = inflater.inflate(R.layout.item_main_record, parent, false);
        ViewHolderRecord entity = new ViewHolderRecord(view);
        view.setTag(entity);

        // 2、填充数据
        RecordEntity r = data.get(i);
        UserEntity user = SQLManage.getUserById(r.getUserId());
        if(null == user){
            entity.userName.setText("--");
        }else {
            entity.userName.setText(user.getUserName());
        }
        entity.icon.setImageResource(r.getIcon());
        entity.money.setText(CommonUtils.MONEY + r.getMoney());
        if (0 == r.getType())
            entity.money.setTextColor(context.getResources().getColor(R.color.red_FF5A5A));

        if (year == r.getYear() && month == r.getMonth() && day == r.getDay()) {
            String time = r.getTime().split(" ")[1];
            entity.time.setText("今日 " + time);
        } else {
            entity.time.setText(r.getTime());
        }

        return view;
    }

    /**
     * 欠款记录视图对应的实体类
     */
    static class ViewHolderRecord {

        ImageView icon; // 欠款记录状态（已还/未还）

        TextView userName, money, time; // 客户姓名

        public ViewHolderRecord(View view) {
            icon = view.findViewById(R.id.main_record_flag);
            userName = view.findViewById(R.id.main_record_user_name);
            money = view.findViewById(R.id.main_record_money);
            time = view.findViewById(R.id.main_record_time);
        }
    }
}
